package com.airplane;

import static org.junit.Assert.*;

import java.util.Collection;
import java.util.Iterator;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.Before;
import org.junit.Test;

public class TestMenuSelector {

	KnowledgeBase kbase;
	StatefulKnowledgeSession ksession;

	@Before
	public void setUp() throws Exception {
		this.kbase = readKnowledgeBase();
		this.ksession = kbase.newStatefulKnowledgeSession();
		AirplaneMenu menu = new AirplaneMenu("Basic 1");
		menu.setIngredient("Beef");
		menu.setIngredient("potatoes");
		menu.setIngredient("egg");
		menu.setIngredient("salad");
		menu.setIngredient("nuts");
		this.ksession.insert(menu);
		menu = new AirplaneMenu("Basic 2");
		menu.setIngredient("Pork");
		menu.setIngredient("rice");
		menu.setIngredient("salad");
		menu.setIngredient("milk");
		this.ksession.insert(menu);
		menu = new AirplaneMenu("Basic 3");
		menu.setIngredient("Fish");
		menu.setIngredient("potatoes");
		menu.setIngredient("salad");
		this.ksession.insert(menu);
		menu = new AirplaneMenu("Muslim");
		menu.setIngredient("Halal beef");
		menu.setIngredient("potatoes");
		menu.setIngredient("egg");
		menu.setIngredient("salad");
		this.ksession.insert(menu);
		menu = new AirplaneMenu("Vegan");
		menu.setIngredient("Rice");
		menu.setIngredient("Soybean"); 
		menu.setIngredient("nuts");
		menu.setIngredient("potatotes");

	}

	public void tearDown() {
		this.ksession.dispose();
	}

	private static KnowledgeBase readKnowledgeBase() throws Exception {
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
				.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newClassPathResource("MenuSelector.drl"),
				ResourceType.DRL);
		KnowledgeBuilderErrors errors = kbuilder.getErrors();
		if (errors.size() > 0) {
			for (KnowledgeBuilderError error : errors) {
				System.err.println(error);
			}
			throw new IllegalArgumentException("Could not parse knowledge.");
		}
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
		return kbase;
	}

	@Test
	public void testFishAllergic() {
		Passenger passenger = new Passenger("Frank the fish allergic","fish");
		this.ksession.insert(passenger);
		ksession.fireAllRules();
		System.out.println(passenger);
		if(passenger.getSelectedMenu().contains(passenger.getAllergy()))
		{
			fail("Passenger got ingredient that causes allergy");
		}
		
	}
	@Test
	public void testEggAllergic() {
		Passenger passenger = new Passenger("Edward the egg allergic","egg");
		this.ksession.insert(passenger);
		ksession.fireAllRules();
		System.out.println(passenger);
		if(passenger.getSelectedMenu().contains(passenger.getAllergy()))
		{
			fail("Passenger got ingredient that causes allergy");
		}
				
	}	
	@Test
	public void testMultipleAllergies() {
		Passenger passenger = new Passenger("Edward the egg allergic","egg");
		this.ksession.insert(passenger);
		passenger = new Passenger("Frank the fish allergic","fish");
		this.ksession.insert(passenger);
		passenger = new Passenger("Nick the nuts allergic","nuts");
		this.ksession.insert(passenger);

		ksession.fireAllRules();
		
        Collection objects=ksession.getObjects(new PeopleFilter());
        
        for (Iterator iter=objects.iterator();iter.hasNext();)
        {
        	passenger=(Passenger) iter.next();
        	System.out.println(passenger);
    		if(passenger.getSelectedMenu().contains(passenger.getAllergy()))
    		{
    			fail("Passenger got ingredient that causes allergy");
    		}
    		        	
        }

	}	
	
	
}
